package hong.jun.task;

import java.util.concurrent.Executors;

/**
 * 说明:线程任务实现抽象类
 * Created on 2023/10/19 14:57
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public abstract class ThreadTask implements Runnable {
    @Override
    public void run() {
        try {
            action();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    protected abstract void action() throws InterruptedException;

    /**
     * @Description: 加入单线程池
     * @Param: []
     * @return: void
     * @Author: yanghongjun
     * @Date: 2023/10/25
     */
    public void addSingleTask() {
        TaskThreadPoolManger.getInstance().addSingleTask(this);
    }

    /**
     * @Description: 加入多线程池
     * @Param: []
     * @return: void
     * @Author: yanghongjun
     * @Date: 2023/10/25
     */
    public void addFixedTask() {
        TaskThreadPoolManger.getInstance().addFixedTask(this);
    }

    public void addVirtualTask() {
        Executors.newVirtualThreadPerTaskExecutor().submit(this);
    }
}
